# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=insight

_gcc_ver=4.8.2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.7
pkgrel=1
pkgdesc="GNU Debugger with Tcl/Tk interface(mingw-w64)"
arch=('any')
url="https://www.gnu.org/software/gdb/"
license=('GPL')
provides=("${MINGW_PACKAGE_PREFIX}-gdb")
conflicts=("${MINGW_PACKAGE_PREFIX}-gdb")
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-libiconv"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-expat"
    "${MINGW_PACKAGE_PREFIX}-libiconv"
    "${MINGW_PACKAGE_PREFIX}-python2"
    "${MINGW_PACKAGE_PREFIX}-tk")
checkdepends=('dejagnu' 'bc')
makedepends=("${MINGW_PACKAGE_PREFIX}-libiconv" "${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-ncurses" "${MINGW_PACKAGE_PREFIX}-xz")
options=('staticlibs' '!distcc' '!ccache')
source=("https://mirrors.kernel.org/sources.redhat.com/gdb/releases/gdb-${pkgver}.tar.bz2"
    "insight.tar.bz2"
    'gdbinit'
    'gdb-fix-display-tabs-on-mingw.patch'
    'gdb-mingw-gcc-4.7.patch'
    'gdb-perfomance.patch'
    'gdb-fix-using-gnu-print.patch'
    'gdb-tcl-m4.patch'
    'gdb-fix-python-stack-corruption.patch'
    'insight-fix-headers.patch')
sha256sums=('0404d1c6ee6b3ddd811722b5197944758d37b4591c216030effbac204f5a6c23'
            '38a73bce7b8b0db9090cc3cf474900b37c81e3ef1228f22d9685002052c879bb'
            '2bbe7eddb1828c394d0ff99777058df79b1a596172603bb0e30d983fc1ea8785'
            'a3e64bf3acc8e92016d493aa1e2fbb5b55c67bfce4359b3578d7a6b5e5d5692a'
            '82ea717b628c8970055a9cbb8edaed8fbebc2cac27467337426dfc4b4c514a6b'
            '6be0f95483e02d5447c93ab5e2c9554d254c6888b1fa3f3a4c011cd6a0a403ad'
            '681a58058b0e92bbd8cecfd116f71892b54f798be9e270b599e15b3ddd0cd0e7'
            'aae6b9029676e1b5ae3515a4d023c127bd1f12f081e4c16328e12323f1d42958'
            '0b9137297fd2028342538ba58448b4bca02b507f7c27a89afba06e60683aa3c4'
            '3ed63ee45725bd1b4aa99bc996c37e9f3633a82264ce5ac49f2f80cd451d1c36')

prepare() {
  cd ${srcdir}
  cp -rf insight/* ${srcdir}/gdb-${pkgver}/
  cd ${srcdir}/gdb-${pkgver}

  # https://sourceware.org/ml/gdb-patches/2013-11/msg00224.html
  #patch -p1 -i ${srcdir}/gdb-fix-display-tabs-on-mingw.patch
  # https://sourceware.org/bugzilla/show_bug.cgi?id=15559
  patch -p1 -i ${srcdir}/gdb-mingw-gcc-4.7.patch
  # https://sourceware.org/bugzilla/show_bug.cgi?id=15412
  patch -p1 -i ${srcdir}/gdb-perfomance.patch

  patch -p1 -i ${srcdir}/gdb-fix-using-gnu-print.patch
  patch -p1 -i ${srcdir}/gdb-tcl-m4.patch
  patch -p1 -i ${srcdir}/gdb-fix-python-stack-corruption.patch
  patch -p1 -i ${srcdir}/insight-fix-headers.patch

  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

  cd libgui
  autoreconf
  cd ../gdb
  aclocal -Ignulib/import/m4
  autoconf
  cd gdbtk
  autoreconf
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  CFLAGS+=" -I${MINGW_PREFIX}/include/tcl8.6/tcl-private/win -I${MINGW_PREFIX}/include/tk8.6/tk-private/win -D__USE_MINGW_ANSI_STDIO=1"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/tcl8.6/tcl-private/win -I${MINGW_PREFIX}/include/tk8.6/tk-private/win -D__USE_MINGW_ANSI_STDIO=1"

  ${srcdir}/gdb-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-targets="i686-w64-mingw32,x86_64-w64-mingw32" \
    --enable-64-bit-bfd \
    --disable-werror \
    --disable-win32-registry \
    --disable-rpath \
    --with-system-gdbinit=${MINGW_PREFIX}/etc/gdbinit \
    --with-python=${MINGW_PREFIX}/bin/python-config.sh \
    --with-tcl=${MINGW_PREFIX}/lib \
    --with-tk=${MINGW_PREFIX}/lib \
    --enable-gdbtk \
    --with-expat \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-zlib \
    --with-lzma \
    --disable-tui

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install

  # Remove unwanted files
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/{man,info}

  # install "custom" system gdbinit
  install -D -m644 ${srcdir}/gdbinit ${pkgdir}${MINGW_PREFIX}/etc/gdbinit
  sed -i 's|%GCC_NAME%|gcc-'${_gcc_ver}'|g' ${pkgdir}${MINGW_PREFIX}/etc/gdbinit

  rm -f ${pkgdir}${MINGW_PREFIX}/include/*.h
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.a

}
